#include"Tcp.hpp"
#include<stdlib.h>
int main(int argc, char *argv[]){
    if(argc!=3){
       LOG(FATAL, "please input port and ip\n");
       exit(0);
    }   
   int port= atoi(argv[1]);
    std::string ip=argv[2];
    int socketd=socket(AF_INET, SOCK_STREAM, 0);
     if (socketd < 0) {
        LOG(FATAL, "socket error\n");
        exit(SOCKET_ERROR);
    } else {
        LOG(DEBUG, "clent socket create success, _socketd: %d\n", socketd);
    }
    struct sockaddr_in local;
    memset(&local, 0, sizeof(local));
    local.sin_family = AF_INET;
    local.sin_port = htons(port);
    local.sin_addr.s_addr = inet_addr(ip.c_str());
    ::inet_pton(AF_INET,ip.c_str(),& local.sin_addr);
    int n1=::connect (socketd,(struct sockaddr*)&local,sizeof(local));
    if(n1<0){
         LOG(DEBUG, "clent error: %d\n", socketd);
    }
      while(1)
    {
        std::string line;
        std::cout << "Please Enter# ";
        std::getline(std::cin, line);
        ssize_t n1 =::write(socketd,line.c_str(),line.size());
        if(n1>0)
        LOG(DEBUG, "clent socket sent success \n");
        else
        LOG(DEBUG, "clent socket sent wrpng, \n");
        char buff[1024];
        ssize_t m= read(socketd,buff,sizeof(buff));
        if(m>0){
        buff[m]=0;
        LOG(DEBUG, "clent socket read right \n");
        std::cout <<"clint:"<<buff<<std::endl;
        }
        else{
        LOG(DEBUG, "clent socket read wrong \n");
        }
    }
}